local jiqiao = fk.CreateSkill {
  name = "lvshi__jiqiao",
  tags = {"Round"},
}

Fk:loadTranslationTable{
  ["lvshi__jiqiao"] = "机巧",
  [":lvshi__jiqiao"] = "轮次技，其他角色的回合结束时，若中央区内的花色数为4，你可选择中央区内不同花色的牌各一张分配给等量名角色。",

  ["#jiqiao-choose"] = "机巧：获得不同花色的牌各一张",
  ["#lvshi__jiqiao-give"] = "机巧：分配给每名角色各一张",

  ["$lvshi__jiqiao1"] = "哼，巧借物力，以取天绸。",
  ["$lvshi__jiqiao2"] = "巧器生花，慧心生计。",
}

local DIY = require "packages/diy_utility/diy_utility"
local ls = require "packages/lvshi/utility"

jiqiao:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

Fk:addPoxiMethod{
  name = "jiqiao",
  card_filter = function(to_select, selected, data)
    if table.contains(data[2], to_select) then return true end
    local suit = Fk:getCardById(to_select).suit
    return table.every(data[2], function (id)
      return Fk:getCardById(id).suit ~= suit
    end)
  end,
  feasible = Util.TrueFunc,
}

jiqiao:addEffect(fk.TurnEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(jiqiao.name) and ls.countSuits(DIY.getCenterCards(player.room)) >= 4
    and player:usedSkillTimes(jiqiao.name, Player.HistoryRound) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = DIY.getCenterCards(room)
    local get = {}
    for _, id in ipairs(cards) do
      local suit = Fk:getCardById(id).suit
      if table.every(get, function (id2)
        return Fk:getCardById(id2).suit ~= suit
      end) then
        table.insert(get, id)
      end
    end
    get = room:askToArrangeCards(player, {
      skill_name = jiqiao.name,
      card_map = cards,
      prompt = "#jiqiao-choose",
      free_arrange = false,
      box_size = 0,
      max_limit = {#cards, 4},
      min_limit = {0, #get},
      poxi_type = "jiqiao",
      default_choice = {{}, get},
    })[2]
    if #get > 0 then
      --room:obtainCard(player, get, true, fk.ReasonPrey, player, jiqiao.name)
      room:askToYiji(player, {
        cards = get,
        min_num = 1,
        max_num = #get,
        single_max = 1,
        targets = room:getAlivePlayers(true),
        skill_name = jiqiao.name,
        expand_pile = get,
        prompt = "#lvshi__jiqiao-give",
        cancelable = false,
      })
    end
  end,
})

return jiqiao
